/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"
#include "at24cxx.h"
#include <string.h>

/* Global typedef */

/* Global define */

/* Global Variable */

void AT24CXX_READALL(void)
{
    uint8_t buf[8] = { 0 };
    for (uint8_t i = 0; i < (256 / 8); i++) {
        uint8_t result = AT24CXX_Read(i * 8, buf, sizeof(buf));
        if (result == 0) {
            printf("read timeout\r\n");
            while (1) {
            }
        }

        printf("ADDR: %02X\t", i * 8);
        for (uint8_t i = 0; i < sizeof(buf); i++) {
            printf("%c\t", buf[i]);
        }
        printf("\r\n");
    }
}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
    printf("This is printf example\r\n");

    char *send_buf = "hello world this is crosspage";

    uint8_t empty = 0xff;

    AT24CXX_Init();

    for (uint16_t i = 0; i < 256; i++) {
        AT24CXX_Write(i, &empty, 1);
        Delay_Ms(5);
    }

    printf("First: =========\r\n");
    AT24CXX_READALL();

    printf("len = %d\r\n", strlen(send_buf));
    AT24CXX_Write_CrossPage(0x0, (uint8_t *)send_buf, strlen(send_buf));

    printf("Second: =========\r\n");
    AT24CXX_READALL();

    while (1) {
    }
}
